//
//  UIImage+Extra.m
//  SLAppProduct
//
//  Created by Jianbo Cai on 2018/3/29.
//  Copyright © 2018年 Sunline. All rights reserved.
//

#import "UIImage+Extra.h"
#import "JFMacro.h"
#import "JFJumpManage.h"
#import "UIColor+AWColor.h"
#import "UIImage+BaseBuddle.h"

@implementation UIImage (Extra)

+ (UIImage *)getImageWithScrollView:(UITableView *)scrollView
{
    CGPoint savedContentOffset = scrollView.contentOffset;
    CGRect savedFrame = scrollView.frame;
    
    scrollView.contentOffset = CGPointZero;
    scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
  
    
    [scrollView reloadData];
        
    UIImage* viewImage = [UIImage convertImageWithView:scrollView];
    
    scrollView.frame = savedFrame;
    scrollView.contentOffset = savedContentOffset;
    
    [scrollView reloadData];
    
//    UIImage* viewImage = nil;
//    UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, 0.0);
//    {
//        CGPoint savedContentOffset = scrollView.contentOffset;
//        CGRect savedFrame = scrollView.frame;
//
//        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
//        scrollView.contentOffset = CGPointZero;
//
//        [scrollView reloadData];
//
//        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
//        viewImage = UIGraphicsGetImageFromCurrentImageContext();
//
//        scrollView.frame = savedFrame;
//        scrollView.contentOffset = savedContentOffset;
//
//        [scrollView reloadData];
//    }
//    UIGraphicsEndImageContext();
    return viewImage;
}

+ (UIImage *)getNavBarTitleImageWithTitle:(NSString *)title {
    UIImage* image = nil;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(SCREEN_WIDTH, 44), NO, 0.0);

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    label.backgroundColor = [[JFJumpManage shareInstance]getCurrentNavVC].navBackGroundColor;
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [[JFJumpManage shareInstance]getCurrentNavVC].navTintColor;

    {
        [label drawViewHierarchyInRect:CGRectMake(0, 0, SCREEN_WIDTH, 44) afterScreenUpdates:YES];
        image = UIGraphicsGetImageFromCurrentImageContext();
    }

    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)componentsJoined:(NSArray *)array {
    CGSize size = CGSizeZero;
    for (UIImage *image in array) {
        size = CGSizeMake(MAX(size.width, image.size.width), size.height + image.size.height);
    }
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGFloat height = 0;
    for (UIImage *image in array) {
        [image drawInRect:CGRectMake(0, height, image.size.width, image.size.height)];
        height += image.size.height;
    }
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resultingImage;
}

- (UIImage *)imageByAppendingImage:(UIImage *)image {
    return [UIImage componentsJoined:[NSArray arrayWithObjects:self, image, nil]];
}

+ (UIImage*)convertImageWithView:(UIView*)v{
    CGSize s = v.bounds.size;
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(s, NO, 0);
    [v.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)shotWithView:(UIView *)view scope:(CGRect)scope {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self convertImageWithView:view].CGImage, scope);
    UIGraphicsBeginImageContext(scope.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, scope.size.width, scope.size.height);
    CGContextTranslateCTM(context, 0, rect.size.height);//下移
    CGContextScaleCTM(context, 1.0f, -1.0f);//上翻
    CGContextDrawImage(context, rect, imageRef);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(imageRef);
//    CGContextRelease(context);
    return image;
}

//获得某个范围内的屏幕图像
+ (UIImage *)imageFromView:(UIView *)theView  atFrame:(CGRect)r
{
    UIGraphicsBeginImageContextWithOptions(r.size, NO, 0.0);
    [theView.layer renderInContext: UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  image;//[self getImageAreaFromImage:theImage atFrame:r];
}

+ (UIImage *)shareBottomQRUrl:(NSString *)url {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    view.backgroundColor = HexColor(@"#ffffff");
    
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    
    //获取app中所有icon名字数组
    NSArray *iconsArr = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
    //取最后一个icon的名字
    NSString *iconLastName = [iconsArr lastObject];
    
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 60, 60)];
    icon.layer.cornerRadius = 5;
    icon.clipsToBounds = YES;
    [icon setImage:[UIImage imageNamed:iconLastName]];
    [view addSubview:icon];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 30, 200, 17)];
    label.text = @"低佣开户，开户即赠股";
    label.textColor = UIColorFromHex(0x181818);
    label.font = [UIFont systemFontOfSize:15];
    [view addSubview:label];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 55, 200, 15)];
    label1.text = @"让金融更简单";
    label1.font = [UIFont systemFontOfSize:13];
    label1.textColor = UIColorFromHex(0xA0A0A0);
    [view addSubview:label1];
    
    UIImageView *QRImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(view.bounds) - 80, 20, 60, 60)];
    if (url) {
        [QRImageView setImage:[self QRImageWithUrl:url]];
    }else {
        [QRImageView setImage:[UIImage imageNameFromBaseBuddle:@"qrCode"]];
    }
    [view addSubview:QRImageView];
    
    return [self convertImageWithView:view];
}


+ (UIImage *)QRImageWithUrl:(NSString *)url {
    // 1. 实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2. 恢复滤镜的默认属性
    [filter setDefaults];

    // 3. 将字符串转换成NSData
    NSString *urlStr = url;//测试二维码地址
    NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding];
    // 4. 通过KVO设置滤镜inputMessage数据
    [filter setValue:data forKey:@"inputMessage"];

    // 5. 获得滤镜输出的图像
    CIImage *outputImage = [filter outputImage];
    
    return [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];//重绘二维码,使其显示清晰
}

/**
 * 根据CIImage生成指定大小的UIImage
 *
 * @param image CIImage
 * @param size 图片宽度
 */
+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}





@end
